import AesUtils from './AesUtils'

const crypto = require('crypto')
export default {
  setConfig:function (res) {
    const diff=(new Date().getTime())-res.timeStamp
    localStorage.setItem('randomString', res.data.randomString)
    localStorage.setItem('timeStampDiff', diff)
  },
  getConfig: function () {
    /**
     * Salt
     * Md5Nums
     */
    return JSON.parse(AesUtils.Decrypt(localStorage.getItem('randomString')))
  },
  md5: function (str) {
    const hash = crypto.createHash('md5')
    hash.update(str)
    return hash.digest('hex')
  },
  generateSign: function (url, Deviceid) {
    url=url.replace(window.ApiDoName,'')
    const config = this.getConfig()
    const diff=localStorage.getItem('timeStampDiff')-0
    const Timestamp = ((new Date()).getTime()-diff) + ''
    let Sign = 'URL=' + url + ',DEVICEID=' + Deviceid + ',TIMESTAMP=' + Timestamp + config.key1
    // console.log('Sign: ',[Sign])
    const nums = config.key2
    // md5次数
    for (let i = 0; i < nums; i++) {
      Sign = this.md5(Sign)
    }
    return {
      Sign,
      TimeStamp: Timestamp,
    }
  }
}
